package com.yc.role.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yc.base.BaseServiceImpl;
import com.yc.role.entity.RoleMenuRelation;
import com.yc.role.mapper.RoleMenuRelationMapper;
import com.yc.role.service.IRoleMenuRelationService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * <p>
 * 后台角色和菜单关系表 服务实现类
 * </p>
 *
 * @author bao
 * @since 2021-12-09
 */
@Service
public class RoleMenuRelationServiceImpl extends BaseServiceImpl<RoleMenuRelationMapper, RoleMenuRelation> implements IRoleMenuRelationService {
    @Override
    public boolean create(RoleMenuRelation roleMenuRelation) {
        return save(roleMenuRelation);
    }

    @Override
    public boolean createBatch(Long roleId, List<RoleMenuRelation> roleMenuRelationList) {
        boolean isOk = false;
        deleteByRoleId(roleId);
        if (!CollectionUtils.isEmpty(roleMenuRelationList)) {
            isOk = saveBatch(roleMenuRelationList);
        }
        return isOk;
    }

    @Override
    public boolean delete(List<Long> ids) {
        return removeByIds(ids);
    }

    @Override
    public boolean update(RoleMenuRelation roleMenuRelation) {
        return updateById(roleMenuRelation);
    }

    @Override
    public RoleMenuRelation queryById(Long id) {
        return getById(id);
    }

    @Override
    public List<RoleMenuRelation> selectList(Long roleId) {
        QueryWrapper<RoleMenuRelation> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(RoleMenuRelation::getRoleId, roleId)
                .orderByDesc(RoleMenuRelation::getId);
        return list(wrapper);
    }

    @Override
    public Page<RoleMenuRelation> selectPage(Integer pageNum, Integer pageSize) {
        QueryWrapper<RoleMenuRelation> wrapper = new QueryWrapper<>();
        wrapper.lambda().orderByDesc(RoleMenuRelation::getId);
        Page<RoleMenuRelation> page = new Page<>(pageNum, pageSize);
        return page(page, wrapper);
    }

    @Override
    public boolean deleteByRoleId(Long roleId) {
        UpdateWrapper<RoleMenuRelation> wrapper = new UpdateWrapper<>();
        wrapper.lambda().set(RoleMenuRelation::getDeleted, 1)
                .eq(RoleMenuRelation::getRoleId, roleId);
        return remove(wrapper);
    }
}
